package com.ctsi.ssdc.service.impl;


import com.ctsi.ssdc.util.GrouponConstant;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallGroupon;
import com.ctsi.ssdc.domain.LitemallGrouponExample;
import com.ctsi.ssdc.service.LitemallGrouponService;
import com.ctsi.ssdc.repository.LitemallGrouponRepository;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallGroupon.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallGrouponServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallGrouponRepository, LitemallGroupon, Integer, LitemallGrouponExample> 
	implements LitemallGrouponService {

    @Autowired
    LitemallGrouponRepository litemallGrouponRepository;


    /**
    * GET  /litemallGroupons : get the litemallGroupons firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallGroupon> findFirstStringColumn(String shareUrl,Pageable pageable){
        String str = shareUrl;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallGrouponExample litemallGrouponExample = new LitemallGrouponExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallGrouponExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallGrouponExample.createCriteria();
        } else{
            litemallGrouponExample.createCriteria().andShareUrlLike("%" + str +"%");
        }
        List<LitemallGroupon>  data = litemallGrouponRepository.selectByExample(litemallGrouponExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallGrouponRepository.countByExample(litemallGrouponExample);
        }
        return new PageResult<LitemallGroupon>(data,count,count);
    }

    @Override
    public LitemallGroupon queryById(Integer userId, Integer grouponId) {
        LitemallGrouponExample example = new LitemallGrouponExample();
        example.or().andIdEqualTo(grouponId).andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return litemallGrouponRepository.selectOneByExample(example);
    }

    @Override
    public LitemallGroupon queryById(Integer id) {
        LitemallGrouponExample example = new LitemallGrouponExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return litemallGrouponRepository.selectOneByExample(example);
    }

    @Override
    public List<LitemallGroupon> queryJoinRecord(Integer linkGrouponId) {
        LitemallGrouponExample example = new LitemallGrouponExample();
        example.or().andGrouponIdEqualTo(linkGrouponId).andStatusNotEqualTo(GrouponConstant.STATUS_NONE).andDeletedEqualTo(false);
        example.orderBy("add_time desc");
        return litemallGrouponRepository.selectByExample(example);
    }

    @Override
    public List<LitemallGroupon> queryMyGroupon(Integer userId) {
        LitemallGrouponExample example = new LitemallGrouponExample();
        example.or().andUserIdEqualTo(userId).andCreatorUserIdEqualTo(userId).andGrouponIdEqualTo(0).andStatusNotEqualTo(GrouponConstant.STATUS_NONE).andDeletedEqualTo(false);
        example.orderBy("add_time desc");
        return litemallGrouponRepository.selectByExample(example);
    }

    @Override
    public List<LitemallGroupon> queryMyJoinGroupon(Integer userId) {
        LitemallGrouponExample example = new LitemallGrouponExample();
        example.or().andUserIdEqualTo(userId).andGrouponIdNotEqualTo(0).andStatusNotEqualTo(GrouponConstant.STATUS_NONE).andDeletedEqualTo(false);
        example.orderBy("add_time desc");
        return litemallGrouponRepository.selectByExample(example);
    }

    @Override
    public int countGroupon(Integer linkGrouponId) {
        LitemallGrouponExample example = new LitemallGrouponExample();
        example.or().andGrouponIdEqualTo(linkGrouponId).andStatusNotEqualTo(GrouponConstant.STATUS_NONE).andDeletedEqualTo(false);
        return (int) litemallGrouponRepository.countByExample(example);
    }

    @Override
    public LitemallGroupon queryByOrderId(Integer id) {
        LitemallGrouponExample example = new LitemallGrouponExample();
        example.or().andOrderIdEqualTo(id).andDeletedEqualTo(false);
        return litemallGrouponRepository.selectOneByExample(example);
    }

    @Override
    public boolean hasJoin(Integer userId, Integer grouponLinkId) {
        LitemallGrouponExample example = new LitemallGrouponExample();
        example.or().andUserIdEqualTo(userId).andGrouponIdEqualTo(grouponLinkId).andStatusNotEqualTo(GrouponConstant.STATUS_NONE).andDeletedEqualTo(false);
        return  litemallGrouponRepository.countByExample(example) != 0;
    }

    @Override
    public int createGroupon(LitemallGroupon groupon) {
        groupon.setAddTime(ZonedDateTime.now());
        groupon.setUpdateTime(ZonedDateTime.now());
        return litemallGrouponRepository.insertSelective(groupon);
    }

    @Override
    public int updateById(LitemallGroupon groupon) {
        groupon.setUpdateTime(ZonedDateTime.now());
        return litemallGrouponRepository.updateByPrimaryKeySelective(groupon);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
